package com.lzh.compiler.parcelerdemo;

import com.lzh.compiler.parceler.Parceler;
import com.lzh.compiler.parceler.annotation.Arg;
import com.lzh.compiler.parcelerdemo.base.BaseAbility;
import com.lzh.compiler.parcelerdemo.utils.Const;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.Text;

import java.util.Set;

public class InjectorAbility extends BaseAbility {
    private Text result;
    private Button toEntity;
    private Button toBundle;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_injector_ablity);
        if (findComponentById(ResourceTable.Id_result) instanceof Text) {
            result = (Text) findComponentById(ResourceTable.Id_result);
        }
        if (findComponentById(ResourceTable.Id_to_entity) instanceof Button) {
            toEntity = (Button) findComponentById(ResourceTable.Id_to_entity);
        }
        if (findComponentById(ResourceTable.Id_to_bundle) instanceof Button) {
            toBundle = (Button) findComponentById(ResourceTable.Id_to_bundle);
        }
        toEntity.setClickedListener(component -> {
            Entity.User user = new Entity.User();
            user.username = "toEntity";
            user.password = Const.DUMMY_USER_PASSWORD;
            IntentParams bundle = Parceler.createFactory(null)
                    .put("user", user)
                    .getBundle();
            Entity entity = Parceler.toEntity(new Entity(), bundle);
            result.setText("toEntity:" + Const.SLACE_R_N + entity.toString());
        });
        toBundle.setClickedListener(component -> {
            Entity.User user = new Entity.User();
            user.username = "toBundle";
            user.password = Const.TO_BUNDLE_PASSWORD;
            Entity entity = new Entity();
            entity.user = user;

            IntentParams bundle = Parceler.toBundle(entity, new IntentParams());
            Set<String> keys = bundle.keySet();
            StringBuilder builder = new StringBuilder("toBundle:");
            for (String key : keys) {
                builder.append(Const.SLACE_R_N);
                builder.append(key + Const.EQUAL + bundle.getParam(key));
            }
            result.setText(builder.toString());
        });
    }

    /**
     * Entity.
     */
    public static class Entity {
        @Arg
        public User user;

        /**
         * User.
         */
        public static class User {
            @Arg
            public String username;
            @Arg
            public String password;

            @Override
            public String toString() {
                return "User{" +
                        "username='" + username + Const.BACK_SLACE +
                        ", password='" + password + Const.BACK_SLACE +
                        '}';
            }
        }

        @Override
        public String toString() {
            return "Entity{" +
                    "user=" + user +
                    '}';
        }
    }
}

